<?php session_start();
    include_once ("../bussiness/CumBR.php");
    include_once ("generales.php");
    if (file_exists("../entity/Constantes.php")){
        include_once ("../entity/Constantes.php");
    }
    if (file_exists("../entity/T_SCOB_MULTA.php")){
        include_once ("../entity/T_SCOB_MULTA.php");
    }
    if(isset($_POST["opc"])){
        
        $id_multa = $_POST["id_multa"];
        $anio = $_POST["anio"];
        $id_sede = $_POST["id_sede"];
        
        if($_POST["opc"]=="bus"){
            $page = $_POST['page'];
            $limit = $_POST['rows'];
            $sidx = $_POST['sidx'];
            $sord = $_POST['sord'];
            
            if (!$sidx){
                $sidx = 1;
            }
            echo mostrarCumGrilla($id_multa,$anio,$id_sede, $page, $limit, $sidx, $sord);
        }
        if($_POST["opc"]=="bus1"){
            $page = $_POST['page'];
            $limit = $_POST['rows'];
            $sidx = $_POST['sidx'];
            $sord = $_POST['sord'];
            
            if (!$sidx){
                $sidx = 1;
            }
            echo mostrarCumDetalleGrilla($id_multa,$anio, $page, $limit, $sidx, $sord);
        }
        if($_POST["opc"]=="bus2"){
            $page = $_POST['page'];
            $limit = $_POST['rows'];
            $sidx = $_POST['sidx'];
            $sord = $_POST['sord'];
            
            if (!$sidx){
                $sidx = 1;
            }
            echo mostrarCumTmpGrilla($id_multa,$anio, $page, $limit, $sidx, $sord);
        }
        if($_POST["opc"]=="bus3"){
            $page = $_POST['page'];
            $limit = $_POST['rows'];
            $sidx = $_POST['sidx'];
            $sord = $_POST['sord'];
            
            if (!$sidx){
                $sidx = 1;
            }
            echo mostrarCumDetalleTmpGrilla($id_multa,$anio, $page, $limit, $sidx, $sord);
        }
    }
    
    function mostrarCumDetalleTmpGrilla($id_multa,$anio, $page, $limit, $sidx, $sord){
        $bus = new CumBR();
        $entity = new T_SCOB_MULTA();
        $entity->ID_MULTA = $id_multa;
        $entity->ANIO = $anio;
        
        $count = $bus->numeroRegistroDetalTmp($id_multa, $anio);
        
        if ($count > 0) {
            $total_pages = ceil($count / $limit);
        } else {
            $total_pages = 0;
        }
        if ($page > $total_pages){
            $page = $total_pages;
        }
        $start = $limit * $page - $limit;
        $responce->page = $page;
        $responce->total = $total_pages;
        $responce->records = $count;
        $i = 0;
        
        $cum = $bus->BuscarSearchDetalTmp($entity, $start, $limit, $sidx, $sord);
        
        if(sizeof($cum)){
            foreach ($cum as $fila){
                $responce->rows[$i]['id'] = $fila->ID_MULTA_DETAL;
                $responce->rows[$i]['cell'] = array($fila->ID_MULTA_DETAL,$fila->ID_MULTA, $fila->ESTADO_MULTA,$fila->NRO_RESOLUCION,$fila->FEC_RESOLUCION,$fila->DES_SIGLA,$fila->ANIO_RESOLUCION);
                $i++;
            }
        }
        return json_encode($responce);
    }
    
    function mostrarCumTmpGrilla($id_multa,$anio, $page, $limit, $sidx, $sord){
        $bus = new CumBR();
        $entity = new T_SCOB_MULTA();
        $entity->ID_MULTA = $id_multa;
        $entity->ANIO = $anio;
        
        $count = $bus->numeroRegistroTmp($id_multa, $anio);
        
        if ($count > 0) {
            $total_pages = ceil($count / $limit);
        } else {
            $total_pages = 0;
        }
        if ($page > $total_pages){
            $page = $total_pages;
        }
        $start = $limit * $page - $limit;
        $responce->page = $page;
        $responce->total = $total_pages;
        $responce->records = $count;
        $i = 0;
        
        $cum = $bus->BuscarSearchTmp($entity, $start, $limit, $sidx, $sord);
        
        if(sizeof($cum)){
            foreach ($cum as $fila){
                $responce->rows[$i]['id'] = $fila->ID_MULTA;
                $responce->rows[$i]['cell'] = array($fila->ID_MULTA,$fila->ANIO, $fila->ID_SEDE,$fila->SEDE,$fila->NRO_FILE_AEC,$fila->ANIO_FILE_AEC,$fila->AUDFECCREACION);
                $i++;
            }
        }
        return json_encode($responce);
    }
    
    function mostrarCumDetalleGrilla($id_multa,$anio, $page, $limit, $sidx, $sord){
        $bus = new CumBR();
        $entity = new T_SCOB_MULTA();
        $entity->ID_MULTA = $id_multa;
        $entity->ANIO = $anio;
        
        $count = $bus->numeroRegistroDetal($id_multa, $anio);
        
        if ($count > 0) {
            $total_pages = ceil($count / $limit);
        } else {
            $total_pages = 0;
        }
        if ($page > $total_pages){
            $page = $total_pages;
        }
        $start = $limit * $page - $limit;
        $responce->page = $page;
        $responce->total = $total_pages;
        $responce->records = $count;
        $i = 0;
        
        $cum = $bus->BuscarSearchDetal($entity, $start, $limit, $sidx, $sord);
        
        if(sizeof($cum)){
            foreach ($cum as $fila){
                $responce->rows[$i]['id'] = $fila->ID_MULTA_DETAL;
                $responce->rows[$i]['cell'] = array($fila->ID_MULTA_DETAL,$fila->ID_MULTA, $fila->ESTADO_MULTA,$fila->ESTADO,$fila->NRO_RESOLUCION,$fila->FEC_RESOLUCION,$fila->DES_SIGLA,$fila->ANIO_RESOLUCION);
                $i++;
            }
        }
        return json_encode($responce);
    }
    
    function mostrarCumGrilla($id_multa,$anio,$id_sede, $page, $limit, $sidx, $sord){
        $bus = new CumBR();
        $entity = new T_SCOB_MULTA();
        $entity->ID_MULTA = $id_multa;
        $entity->ANIO = $anio;
        $entity->ID_SEDE = $id_sede;
        
        $count = $bus->numeroRegistro($id_multa, $anio, $id_sede);
        
        if ($count > 0) {
            $total_pages = ceil($count / $limit);
        } else {
            $total_pages = 0;
        }
        if ($page > $total_pages){
            $page = $total_pages;
        }
        $start = $limit * $page - $limit;
        $responce->page = $page;
        $responce->total = $total_pages;
        $responce->records = $count;
        $i = 0;
        
        $cum = $bus->BuscarSearch($entity, $start, $limit, $sidx, $sord);
        
        if(sizeof($cum)){
            foreach ($cum as $fila){
                $responce->rows[$i]['id'] = $fila->ID_MULTA;
                $responce->rows[$i]['cell'] = array($fila->ID_MULTA,$fila->ANIO, $fila->ID_SEDE,$fila->SEDE,$fila->NRO_FILE_AEC,$fila->ANIO_FILE_AEC,$fila->AUDFECCREACION);
                $i++;
            }
        }
        return json_encode($responce);
    }
?>